package com.xyauto.theme.view.seekbar;


import android.animation.Animator;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.PixelFormat;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ClipDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.graphics.drawable.NinePatchDrawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.StateListDrawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.graphics.drawable.shapes.Shape;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AnticipateOvershootInterpolator;
import android.view.animation.Interpolator;
import android.view.animation.LinearInterpolator;
import android.widget.RelativeLayout.LayoutParams;

import com.xyauto.theme.view.seekbar.interfaces.ISeekBar;

@SuppressLint("NewApi")
public class SeekBarEx extends BaseSeekBar implements ISeekBar {
    private static final String TAG = "SeekBarEx";
    private static final int MAX_LEVEL = 10000;
    private Drawable mDrawableHight;
    private Interpolator mInterpolator;
    private OnSeekBarChangeListener mOnSeekBarChangeListener;
    Bitmap mSampleTile;
    private Drawable initmThumb;
    private int initmThumbheight;
    private int initmThumbwidth;
    int mThumbOffset;
    private int mViewHeight;
    private int mViewWith;
    private ValueAnimator valueAnimator;
    private static final int VALUEX = 45645;
    private int MARGINLEFTOFFSET = 0;
//    private int progresstype;

    private int mTouchStatus; //0-------normal,1---------touch,2--------drag
    private int mTouchMoveCount;
    private float mTouchPressX;
    private float mTouchPressY;
    private int mAnimationRun;
    private int mLayoutResize;

    private int headshinemargin;
    private int tailshinemargin;
    private int hlheadshinemargin;
    private int hltailshinemargin;
    private int mActualWidth;
    private int mActualHeight;

    private Context context;
    private int mReinitThumbWidth;
    private int mReinitThumbHeight;

    private boolean mProgressDisable = true;

    private boolean mIsNine = false;
    private final Handler handler = new Handler(Looper.myLooper()) {
        public void handleMessage(Message msg) {
            if (msg.what == VALUEX) {
                mAnimationRun = 0;
            }
        }
    };

    public SeekBarEx(Context paramContext) {
        this(paramContext, null);
        mProgressDisable = true;
    }

    public SeekBarEx(Context paramContext, AttributeSet paramAttributeSet) {
        this(paramContext, paramAttributeSet, 0);
        mProgressDisable = true;
    }

    public SeekBarEx(Context paramContext, AttributeSet paramAttributeSet, int paramInt) {
        this(paramContext, paramAttributeSet, paramInt, 0);
        mProgressDisable = true;
    }

    public SeekBarEx(Context paramContext, AttributeSet paramAttributeSet, int paramInt1, int paramInt2) {
        super(paramContext, paramAttributeSet, paramInt1);
        mProgressDisable = true;
        initViews(paramContext, paramAttributeSet, paramInt1, paramInt2);
    }

    @Override
    protected void refreshDrawable() {
        onSizeChanged(getWidth(),getHeight(),getWidth(),getHeight());
    }

    public void setSeekBarExEnable(boolean enable) {
        mProgressDisable = enable;
    }

    public static Bitmap DrawableToBitmap(Drawable drawable) {
        int width = drawable.getIntrinsicWidth();
        int heigh = drawable.getIntrinsicHeight();
        drawable.setBounds(0, 0, width, heigh);
        Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Config.ARGB_8888
                : Config.RGB_565;
        Bitmap bitmap = Bitmap.createBitmap(width, heigh, config);
        Canvas canvas = new Canvas(bitmap);
        drawable.draw(canvas);
        return bitmap;
    }

    private void initViews(Context paramContext, AttributeSet paramAttributeSet, int paramInt1, int paramInt2) {
        mTouchStatus = 0;
        mAnimationRun = 0;
        mLayoutResize = 0;
        this.context = paramContext;
        init(paramContext, paramAttributeSet, paramInt1, paramInt2);

        if (getProgresstype() == 1) {
            if (getThumb() != null) {
                setThumb(new BitmapDrawable(context.getResources(),
                        createRotateDrawable(getThumb())));
            }
        }
        initmThumb = getThumb();
        if(initmThumb != null) {
            initmThumbwidth = getThumb().getIntrinsicWidth();
            initmThumbheight = getThumb().getIntrinsicHeight();
        }


        initAnimation();
    }

    private void setHightlight(int paramInt1, int paramInt2, int paramInt3, int paramInt4) {
        // paramInt1 x
        if (mIsNine) {
            if (mDrawableHight != null) {
                if (getProgresstype() == 1) {
                    if (getDirection() == 0) {
                        int bottom;
                        int top = paramInt1 - mActualHeight / 2 - headshinemargin;
                        if (top + mActualHeight >= (mViewHeight - MARGINLEFTOFFSET)) {
                            top = mViewHeight - MARGINLEFTOFFSET - mActualHeight;
                        } else if (top <= (0 + MARGINLEFTOFFSET)) {
                            top = MARGINLEFTOFFSET - headshinemargin;
                        }
                        top += mActualHeight / 2 - this.hlheadshinemargin / 2;
                        bottom = mViewHeight - MARGINLEFTOFFSET - this.hltailshinemargin - headshinemargin;
                        this.mDrawableHight.setBounds(0, top, paramInt2, bottom);
                    } else {
                        int top = paramInt1 - mActualHeight / 2 - headshinemargin;
                        if (top + mActualHeight >= (mViewHeight - MARGINLEFTOFFSET)) {
                            top = mViewHeight - MARGINLEFTOFFSET - mActualHeight;
                        } else if (top <= (0 + MARGINLEFTOFFSET)) {
                            top = MARGINLEFTOFFSET - headshinemargin;
                        }
                        top += mActualHeight / 2 - this.hlheadshinemargin / 2 - headshinemargin;
                        this.mDrawableHight.setBounds(0, 0 + MARGINLEFTOFFSET + this.hltailshinemargin, paramInt2, top);
                    }
                } else {
                    if (getDirection() == 1) {
                        int right;
                        int left = paramInt1 - mActualWidth / 2 - headshinemargin;
                        if (left + mActualWidth >= (mViewWith - MARGINLEFTOFFSET)) {
                            left = mViewWith - MARGINLEFTOFFSET - mActualWidth;
                        } else if (left <= (0 + MARGINLEFTOFFSET)) {
                            left = MARGINLEFTOFFSET - headshinemargin;
                        }
                        left += mActualWidth / 2 - this.hlheadshinemargin / 2;
                        right = mViewWith - MARGINLEFTOFFSET - this.hltailshinemargin - headshinemargin;
                        this.mDrawableHight.setBounds(left, 0, right, paramInt2);
                    } else {
                        int right = paramInt1 - mActualWidth / 2 - headshinemargin;
                        if (right + mActualWidth >= (mViewWith - MARGINLEFTOFFSET)) {
                            right = mViewWith - MARGINLEFTOFFSET - mActualWidth / 2;
                        } else if (right <= (0 + MARGINLEFTOFFSET)) {
                            right = MARGINLEFTOFFSET - headshinemargin - tailshinemargin;
                        }
                        right += mActualWidth / 2 - this.hlheadshinemargin / 2 - headshinemargin;
                        this.mDrawableHight.setBounds(0 + MARGINLEFTOFFSET + this.hltailshinemargin, 0, right, paramInt2);
                    }
                }
                //invalidate();
            }
        } else {
            if (mActualHeight >= 0 && mActualWidth >= 0) {
                if (getProgresstype() == 1) {

                    float rate = (mViewHeight - 2 * MARGINLEFTOFFSET - mActualHeight + headshinemargin + tailshinemargin) * 1.0f / (mViewHeight - 2 * MARGINLEFTOFFSET);

                    paramInt1 = ((int) ((paramInt1 - MARGINLEFTOFFSET) * rate) + (mActualHeight - headshinemargin - tailshinemargin) / 2 + MARGINLEFTOFFSET);

                } else {
                    float rate = (mViewWith - 2 * MARGINLEFTOFFSET - mActualWidth + headshinemargin + tailshinemargin) * 1.0f / (mViewWith - 2 * MARGINLEFTOFFSET);

                    paramInt1 = ((int) ((paramInt1 - MARGINLEFTOFFSET) * rate) + (mActualHeight - headshinemargin - tailshinemargin) / 2 + MARGINLEFTOFFSET);
                }
            }

            if (mDrawableHight != null) {
                if (getThumb() == null) {
                    headshinemargin = 0;
                    tailshinemargin = 0;
                    //MARGINLEFTOFFSET=0;
                }
                if (getProgresstype() == 1) {

                    int top = paramInt1;
                    if (top >= (mViewHeight - MARGINLEFTOFFSET)) {
                        top = mViewHeight - MARGINLEFTOFFSET - (mActualHeight - headshinemargin - tailshinemargin) / 2;
                    } else if (top <= (0 + MARGINLEFTOFFSET)) {
                        top = MARGINLEFTOFFSET + (mActualHeight - headshinemargin - tailshinemargin) / 2;
                    }

                    if ((this.mDrawableHight != null) && ((this.mDrawableHight instanceof ClipDrawable))) {

                        this.mDrawableHight.setLevel(Math.round(toProgress(top) * MAX_LEVEL / getMax()));

                    }
                } else {


                    int left = paramInt1;
                    if (left >= (mViewWith - MARGINLEFTOFFSET)) {
                        left = mViewWith - MARGINLEFTOFFSET - (mActualWidth - headshinemargin - tailshinemargin) / 2;
                    } else if (left <= (0 + MARGINLEFTOFFSET)) {
                        left = MARGINLEFTOFFSET + (mActualWidth - headshinemargin - tailshinemargin) / 2;
                    }


                    //this.mDrawableHight.setBounds(left,0,right,paramInt2);
                    this.mDrawableHight.setLevel(Math.round(toProgress(left) * MAX_LEVEL / getMax()));

                }
                //invalidate();
            }
        }


//		if ((this.mDrawableHight != null) && ((this.mDrawableHight instanceof ClipDrawable))) {
//			//((ClipDrawable) this.mDrawableHight).setLevel(MAX_LEVEL);
//			this.mDrawableHight.setLevel(mProgress*MAX_LEVEL/mMax);
//			
//			Log.i("seekbarex", "onDraw mProgress="+mProgress);	
//		}
    }

    /**
     * Converts a drawable to a tiled version of itself. It will recursively
     * traverse layer and state list drawables.
     */
    private Drawable tileify(Drawable drawable, int width, int height, boolean clip) {
        if (drawable instanceof LayerDrawable) {
            LayerDrawable background = (LayerDrawable) drawable;
            final int N = background.getNumberOfLayers();
            Drawable[] outDrawables = new Drawable[N];

            LayerDrawable newBg = new LayerDrawable(outDrawables);

            for (int i = 0; i < N; i++) {
                newBg.setId(i, background.getId(i));
            }
            return newBg;
        } else if (drawable instanceof StateListDrawable) {
            StateListDrawable in = (StateListDrawable) drawable;
            StateListDrawable out = new StateListDrawable();
            return out;
        } else if (drawable instanceof BitmapDrawable) {
            float scaleX = 0;
            float scaleY = 0;
            Bitmap tileBitmap = ((BitmapDrawable) drawable).getBitmap();
            int bitmapWidth = tileBitmap.getWidth();
            int bitmapHeight = tileBitmap.getHeight();

            int realWith = mViewWith - MARGINLEFTOFFSET * 2;
            if (getProgresstype() == 1)
                realWith = mViewWith;
            if (realWith < bitmapWidth) {
                scaleX = (float) realWith / (float) bitmapWidth;
            } else if (mViewWith > bitmapWidth) {
                scaleX = (float) realWith / (float) bitmapWidth;
            } else {
                scaleX = 1.0f;
            }
            //Log.i("seekbarex", "tileify mViewWith="+mViewWith+",bitmapWidth="+bitmapWidth+",scaleX ="+scaleX);
            Matrix matrix = new Matrix();
            if (getProgresstype() == 1) {
                int realHeight = getHeight() - MARGINLEFTOFFSET * 2;
                if (bitmapHeight < realHeight) {
                    scaleY = (float) realHeight / bitmapHeight;
                    matrix.setScale(scaleX, scaleY);
                    tileBitmap = Bitmap.createBitmap(tileBitmap, 0, 0, width, height, matrix, false);
                } else if (bitmapHeight == realHeight) {
                    scaleY = (float) realHeight / bitmapHeight;
                    matrix.setScale(scaleX, 1.0f);
                    tileBitmap = Bitmap.createBitmap(tileBitmap, 0, 0, width, height, matrix, false);
                } else {
                    scaleY = (float) realHeight / bitmapHeight;
                    matrix.setScale(scaleX, scaleY);
                    tileBitmap = Bitmap.createBitmap(tileBitmap, 0, 0, tileBitmap.getWidth(), tileBitmap.getHeight(), matrix, false);
                }
                //Log.i("seekbarex", "tileify realHeight="+realHeight+",bitmapHeight="+bitmapHeight+",scaleX ="+scaleX);
            } else {
                if (bitmapHeight < getHeight()) {
                    scaleY = (float) getHeight() / bitmapHeight;
                    matrix.setScale(scaleX, scaleY);
                    tileBitmap = Bitmap.createBitmap(tileBitmap, 0, 0, width, height, matrix, false);
                } else if (bitmapHeight == getHeight()) {
                    scaleY = (float) getHeight() / bitmapHeight;
                    matrix.setScale(scaleX, 1.0f);
                    tileBitmap = Bitmap.createBitmap(tileBitmap, 0, 0, width, height, matrix, false);
                } else {
                    scaleY = (float) getHeight() / bitmapHeight;
                    matrix.setScale(scaleX, scaleY);
                    tileBitmap = Bitmap.createBitmap(tileBitmap, 0, 0, tileBitmap.getWidth(), tileBitmap.getHeight(), matrix, false);
                }
            }

            if (mSampleTile == null) {
                mSampleTile = tileBitmap;
            }
            final ShapeDrawable shapeDrawable = new ShapeDrawable(getDrawableShape());

            final BitmapShader bitmapShader = new BitmapShader(tileBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
            shapeDrawable.getPaint().setShader(bitmapShader);

            if (getProgresstype() == 1) {

                if (getDirection() == 0) {
                    return (clip) ? new ClipDrawable(shapeDrawable, Gravity.BOTTOM, ClipDrawable.VERTICAL) : shapeDrawable;
                } else {
                    return (clip) ? new ClipDrawable(shapeDrawable, Gravity.TOP, ClipDrawable.VERTICAL) : shapeDrawable;
                }
            } else {
                if (getDirection() == 0) {
                    return (clip) ? new ClipDrawable(shapeDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL) : shapeDrawable;
                } else {
                    return (clip) ? new ClipDrawable(shapeDrawable, Gravity.RIGHT, ClipDrawable.HORIZONTAL) : shapeDrawable;
                }
            }

        } else if (drawable instanceof NinePatchDrawable) {
            mIsNine = true;
            drawable.setLevel(MAX_LEVEL);
            drawable.setDither(false);
        }
        return drawable;
    }

    private void setThumbPos(int w, Drawable thumb) {

        int left = 0, top = 0, right = 0, bottom = 0;

        if (getNewStyle() == 1 && !mIsNine) {

            if (getProgresstype() == 1) {
                if (w > MARGINLEFTOFFSET && w < mViewHeight - MARGINLEFTOFFSET) {
                    float rate = (mViewHeight - 2 * MARGINLEFTOFFSET - mActualHeight + headshinemargin + tailshinemargin) * 1.0f / (mViewHeight - 2 * MARGINLEFTOFFSET);

                    w = ((int) ((w - MARGINLEFTOFFSET) * rate) + (mActualHeight - headshinemargin - tailshinemargin) / 2 + MARGINLEFTOFFSET);

//					if( this.mDirection == 0 )
//					{
//						w = ((int)((w-MARGINLEFTOFFSET)*rate)+mActualHeight/2-tailshinemargin+MARGINLEFTOFFSET);
//					}
//					else
//					{
//						w = ((int)((w-MARGINLEFTOFFSET)*rate)+mActualHeight/2-headshinemargin+MARGINLEFTOFFSET);
//					}

                }
                //if( this.mDirection == 0 )
                {
                    left = (mViewWith - mActualWidth) / 2;
                    right = left + mActualWidth;
                    //top = w - mActualHeight/2-tailshinemargin;
                    top = w - (mActualHeight + headshinemargin - tailshinemargin) / 2;
                    if ((top + mActualHeight - tailshinemargin) >= (mViewHeight - MARGINLEFTOFFSET)) {
                        top = mViewHeight - MARGINLEFTOFFSET - mActualHeight + tailshinemargin;
                    } else if (top <= MARGINLEFTOFFSET - headshinemargin) {
                        top = MARGINLEFTOFFSET - headshinemargin;
                    }
                    bottom = top + mActualHeight;
                }
//				else
//				{
//					left = (mViewWith - mActualWidth)/2;
//					right = left + mActualWidth;
//					top = w - (mActualHeight+headshinemargin-tailshinemargin)/2 ;
//					if ((top + mActualHeight -headshinemargin) >= (mViewHeight - MARGINLEFTOFFSET)) {
//						top = mViewHeight - MARGINLEFTOFFSET - mActualHeight +headshinemargin;
//					} else if (top <= MARGINLEFTOFFSET-headshinemargin) {
//						top = MARGINLEFTOFFSET - headshinemargin ;
//					}		
//					bottom = top + mActualHeight;
//				}
            } else {


                if (w > MARGINLEFTOFFSET && w < mViewWith - MARGINLEFTOFFSET) {

                    float rate = (mViewWith - 2 * MARGINLEFTOFFSET - mActualWidth + headshinemargin + tailshinemargin) * 1.0f / (mViewWith - 2 * MARGINLEFTOFFSET);

                    w = ((int) ((w - MARGINLEFTOFFSET) * rate) + (mActualWidth - headshinemargin - tailshinemargin) / 2 + MARGINLEFTOFFSET);


                }
                //if( this.mDirection == 0 )
                {
                    top = (mViewHeight - mActualHeight) / 2;
                    bottom = top + mActualHeight;

                    //Log.i("seekbarex", "kao "+w+" "+(mActualWidth+headshinemargin-tailshinemargin)/2+" "+headshinemargin+" "+tailshinemargin);
                    left = w - (mActualWidth + headshinemargin - tailshinemargin) / 2;

                    //Log.i("seekbarex", "kao2 "+mActualWidth +" "+left+" "+mViewWith);

                    if ((left + mActualWidth - tailshinemargin) >= (mViewWith - MARGINLEFTOFFSET)) {
                        left = mViewWith - MARGINLEFTOFFSET - mActualWidth + tailshinemargin;
                    } else if (left < MARGINLEFTOFFSET - headshinemargin) {
                        left = MARGINLEFTOFFSET - headshinemargin;
                    }
                    right = left + mActualWidth;
                }
//				else
//				{
//					top = (mViewHeight - mActualHeight)/2;
//					bottom = top + mActualHeight;
//					left = w - (mActualWidth-headshinemargin+tailshinemargin)/2 ;
//					
//					if ((left + mActualWidth-headshinemargin) >= (mViewWith - MARGINLEFTOFFSET)) {
//						left = mViewWith - MARGINLEFTOFFSET - mActualWidth  + tailshinemargin;
//					} else if (left<MARGINLEFTOFFSET) {
//						left = MARGINLEFTOFFSET - tailshinemargin;
//					}
//					right = left + mActualWidth ;
//				}

            }
        } else {
            if (getProgresstype() == 1) {
//				left = (mViewWith - mActualWidth)/2;
//				right = left + mActualWidth;
//				top = w - mActualHeight/2 - headshinemargin;
//				if ((top + mActualHeight/2 + tailshinemargin + headshinemargin) >= (mViewHeight - MARGINLEFTOFFSET)) {
//					top = mViewHeight - MARGINLEFTOFFSET - mActualHeight + mActualHeight/2-3;
//				} else if ((top + mActualHeight/2) <= (0 + MARGINLEFTOFFSET)) {
//					top = MARGINLEFTOFFSET - headshinemargin - mActualHeight/2+3;
//				}		
//				bottom = top + mActualHeight + tailshinemargin - headshinemargin;


                left = (mViewWith - mActualWidth) / 2;
                right = left + mActualWidth;
                //top = w - mActualHeight/2-tailshinemargin;
                top = w - (mActualHeight + headshinemargin - tailshinemargin) / 2;
                if ((top + mActualHeight - tailshinemargin) >= (mViewHeight - MARGINLEFTOFFSET)) {
                    top = mViewHeight - MARGINLEFTOFFSET - mActualHeight + tailshinemargin;
                } else if (top <= MARGINLEFTOFFSET - headshinemargin) {
                    top = MARGINLEFTOFFSET - headshinemargin;
                }
                bottom = top + mActualHeight;


            } else {
                top = (mViewHeight - mActualHeight) / 2;
                bottom = top + mActualHeight;
                left = w - mActualWidth / 2 - headshinemargin;
                if ((left + tailshinemargin + headshinemargin) >= (mViewWith - MARGINLEFTOFFSET)) {
                    left = mViewWith - MARGINLEFTOFFSET - mActualWidth + mActualWidth / 2 - 3;
                } else if ((left + mActualWidth / 2) <= (0 + MARGINLEFTOFFSET)) {
                    left = MARGINLEFTOFFSET - headshinemargin - mActualWidth / 2 + 3;
                }
                right = left + mActualWidth + tailshinemargin - headshinemargin;
            }
        }
        thumb.setBounds(left, top, right, bottom);
    }

    private Bitmap createRotateDrawable(Drawable thumb) {
        try {
            if (thumb instanceof BitmapDrawable) {
                Bitmap bitmapSrc = ((BitmapDrawable) (thumb)).getBitmap();
                Matrix matrix = new Matrix();
                matrix.setRotate(-90, (float) bitmapSrc.getWidth() / 2, (float) bitmapSrc.getHeight() / 2);

                Bitmap bm1 = Bitmap.createBitmap(bitmapSrc, 0, 0, bitmapSrc.getWidth(), bitmapSrc.getHeight(), matrix, true);
                return bm1;
            } else
                return null;
        } catch (OutOfMemoryError e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
	/*private Bitmap createBitmap(Drawable thumb, int mViewHeight) {
		Bitmap bitmapSrc = ((BitmapDrawable) (thumb)).getBitmap();
		float s = mViewHeight / (float) thumb.getIntrinsicHeight();
		Matrix matrix = new Matrix();
		matrix.setScale(s, s);
		bitmapSrc = Bitmap.createBitmap(bitmapSrc, 0, 0, thumb.getIntrinsicWidth(), thumb.getIntrinsicHeight(), matrix, false);
		bitmap = Bitmap.createBitmap(mViewHeight, mViewHeight, Config.ARGB_8888);
		Canvas canvas = new Canvas(bitmap);
		int sw = bitmapSrc.getWidth() / 2;
		int ssw = mViewHeight / 2;
		int left_x = ssw - sw;
		canvas.drawBitmap(bitmapSrc, left_x, 0, new Paint());
		return bitmap;
	}*/

    private float toPX(int progress) {
        float px;
        if (getProgresstype() == 1) {
            int actProcess = progress;
            if (getDirection() == 0) {
                actProcess = getMax() - progress;
            }
            px = (float) (this.mViewHeight - MARGINLEFTOFFSET * 2) * (float) (actProcess) / (float) getMax() + MARGINLEFTOFFSET;
        } else {
            int actProcess = progress;
            if (getDirection() == 1) {
                actProcess = getMax() - progress;
            }
            px = (float) (this.mViewWith - MARGINLEFTOFFSET * 2) * (float) (actProcess) / (float) getMax() + MARGINLEFTOFFSET;
        }
        return px;
    }

    private float toProgress(float px) {

        //Log.i("seekbarex", "toProgress px"+px+" "+this.mViewHeight+this.mDirection+this.progresstype);

        float xOffset = px;
        int length;
        if (getProgresstype() == 1) {
            length = this.mViewHeight;
            if (getDirection() == 0) {
                xOffset = this.mViewHeight - px;
            }
        } else {
            length = this.mViewWith;
            if (getDirection() == 1) {
                xOffset = this.mViewWith - px;
            }
        }
        if (xOffset <= MARGINLEFTOFFSET) {

            //Log.i("seekbarex", "toProgressxOffset"+xOffset+" "+MARGINLEFTOFFSET);
            return 0;
        }
        if (xOffset >= (length - MARGINLEFTOFFSET)) {
            //Log.i("seekbarex", "toProgressxOffset max");
            return getMax();
        }
        //Log.i("seekbarex", "toProgress xOffset="+xOffset+","+(this.mMax * (float) (xOffset-MARGINLEFTOFFSET) / (float) (this.mViewHeight - MARGINLEFTOFFSET * 2)));
        return getMax() * (float) (xOffset - MARGINLEFTOFFSET) / (float) (length - MARGINLEFTOFFSET * 2);
    }

    void onStartTrackingTouch(MotionEvent paramMotionEvent) {
        if (mProgressDisable == false)
            return;
        setPressed(true);
        mTouchStatus = 1;
        mTouchMoveCount = 0;
        mTouchPressX = paramMotionEvent.getX();
        mTouchPressY = paramMotionEvent.getY();
        //Log.i("seekbarex", "onTouchEvent ACTION_DOWN="+mTouchPressX+","+mTouchPressY+",max ="+this.mMax+",(int)toProgress(paramInt) = "+ Math.round(toProgress((int) paramMotionEvent.getY())));
        if (this.mOnSeekBarChangeListener != null) {
            this.mOnSeekBarChangeListener.onStartTrackingTouch(this);
        }
    }

    private void trackTouchEvent(MotionEvent paramMotionEvent) {
        if (mProgressDisable == false)
            return;
        if ((Math.abs((int) (paramMotionEvent.getX() - mTouchPressX)) <= 16)
                && (Math.abs((int) (paramMotionEvent.getY() - mTouchPressY)) <= 16)) {
            if (mTouchMoveCount > 10) {//long press ,update
                mTouchStatus = 2;
                if (getProgresstype() == 1) {
                    drawProgress(paramMotionEvent.getY());
                } else {
                    drawProgress(paramMotionEvent.getX());
                }
            } else
                mTouchMoveCount++;
            return;
        }
        //Log.i("seekbarex", "onTouchEvent ACTION_MOVE="+mTouchPressX+","+mTouchPressY+",mTouchStatus ="+mTouchStatus);
        mTouchStatus = 2;
        if (getProgresstype() == 1) {
            drawProgress(paramMotionEvent.getY());
        } else {
            drawProgress(paramMotionEvent.getX());
        }
    }

    private void trackTouchEvent_up(final MotionEvent paramMotionEvent) {
        if (mProgressDisable == false)
            return;
        setPressed(false);
        //mEffect = 0;
        //Log.i("seekbarex", "trackTouchEvent_up="+getProgress()+",(int)toProgress(paramInt) = "+paramMotionEvent.getY());
        if ((mTouchStatus == 2) || (getEffect() == 0)) {
            //Log.i("seekbarex", "onTouchEvent mTouchStatus = 2="+getProgress()+",(int)toProgress(paramInt) = "+(int)toProgress((int) paramMotionEvent.getX()));
            handler.removeMessages(SeekBarEx.VALUEX);
            mAnimationRun = 1;
            if (getProgresstype() == 1) {
                setProgress(Math.round(toProgress(paramMotionEvent.getY())));
                updateProgressBackgroud((int) toPX(getProgress()), getWidth(), 0, 0);
            } else {
                setProgress(Math.round(toProgress(paramMotionEvent.getX())));
                updateProgressBackgroud((int) toPX(getProgress()), getHeight(), 0, 0);
            }
            //Log.i("seekbarex", "trackTouchEvent_up="+(int)toPX(this.mProgress)+",max ="+this.mMax+",this.mProgress = "+ this.mProgress);
            onStopTrackingTouch();
            handler.sendEmptyMessageDelayed(SeekBarEx.VALUEX, 350);
            return;
        }
        if (getProgresstype() == 1)
            startProgressAnimation(paramMotionEvent.getY(), getProgresstype());
        else
            startProgressAnimation(paramMotionEvent.getX(), getProgresstype());
    }

    private void updateProgressBackgroud(int paramInt1, int paramInt2, int paramInt3, int paramInt4) {
        if (getThumb() != null) {
            setThumbPos(paramInt1, getThumb()); // paramInt1:w
        }
        if (this.mDrawableHight != null) { // paramInt2 h
            setHightlight(paramInt1, paramInt2, paramInt3, paramInt4);
        }
        invalidate();
    }

    public void changeProgress(int paramInt) {
        this.mDrawableHight.setBounds(0, 0, paramInt, getHeight());
        invalidate();
    }

    protected void drawableStateChanged() {
        super.drawableStateChanged();

        if ((this.mDrawableHight != null) && (this.mDrawableHight.isStateful())) {
            int[] arrayOfInt = getDrawableState();

            this.mDrawableHight.setState(arrayOfInt);
        }
        invalidate();
    }

    Shape getDrawableShape() {
        return new RoundRectShape(new float[]{0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F}, null, null);
    }


    public boolean isLayoutRtl() {
        return getLayoutDirection() == View.LAYOUT_DIRECTION_RTL;
//        return getLayoutDirection() == 1;
    }

    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
    }

    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (this.mDrawableHight != null) {
            this.mDrawableHight.setVisible(false, false);
        }
    }

    protected void onDraw(Canvas paramCanvas) {
        super.onDraw(paramCanvas);

        if (getBackground() != null) {
            getBackground().draw(paramCanvas);
        }
        if (this.mDrawableHight != null) {
            this.mDrawableHight.draw(paramCanvas);
        }
        if (getThumb() != null) {
            getThumb().draw(paramCanvas);
        }
    }

    protected void onMeasure(int paramInt1, int paramInt2) {
        super.onMeasure(paramInt1, paramInt2);
        this.mViewWith = getMeasuredWidth();
        this.mViewHeight = getMeasuredHeight();
        if (mLayoutResize == 0) {
            LayoutParams fontSize;
            fontSize = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
//            GlaNative.getInstance().calculateLayout(context, fontSize, new Rect(0, 0, 0, this.MARGINLEFTOFFSET));     TODO
            if (getThumb() != null) {
                mActualWidth = this.mReinitThumbWidth = getThumb().getIntrinsicWidth();
                mActualHeight = this.mReinitThumbHeight = getThumb().getIntrinsicHeight();
            }
            //this.MARGINLEFTOFFSET = fontSize.height; TODO
            if (getProgresstype() == 1) {
//                GlaNative.getInstance().calculateLayout(context, fontSize, new Rect(0, this.headshinemargin, 0, 0));TODO
                this.headshinemargin = fontSize.topMargin;
//                GlaNative.getInstance().calculateLayout(context, fontSize, new Rect(0, this.tailshinemargin, 0, 0));TODO
                this.tailshinemargin = fontSize.topMargin;
//                GlaNative.getInstance().calculateLayout(context, fontSize, new Rect(0, this.hlheadshinemargin, 0, 0));TODO
                this.hlheadshinemargin = fontSize.topMargin;
//                GlaNative.getInstance().calculateLayout(context, fontSize, new Rect(0, this.hltailshinemargin, 0, 0));TODO
                this.hltailshinemargin = fontSize.topMargin;


            } else {
//                GlaNative.getInstance().calculateLayout(context, fontSize, new Rect(this.headshinemargin, 0, 0, 0));  TODO
                this.headshinemargin = fontSize.leftMargin;
//                GlaNative.getInstance().calculateLayout(context, fontSize, new Rect(this.tailshinemargin, 0, 0, 0));  TODO
                this.tailshinemargin = fontSize.leftMargin;
//                GlaNative.getInstance().calculateLayout(context, fontSize, new Rect(this.hlheadshinemargin, 0, 0, 0));    TODO
                this.hlheadshinemargin = fontSize.leftMargin;
//                GlaNative.getInstance().calculateLayout(context, fontSize, new Rect(this.hltailshinemargin, 0, 0, 0));    TODO
                this.hltailshinemargin = fontSize.leftMargin;
            }
            mLayoutResize = 1;
        }
    }

    public void reOnMeasure() {
        this.mViewWith = getMeasuredWidth();
        this.mViewHeight = getMeasuredHeight();
        LayoutParams fontSize;
        fontSize = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
//        GlaNative.getInstance().calculateLayout(context, fontSize, new Rect(0, 0, 0, this.mReinitMARGINLEFTOFFSET));
        this.MARGINLEFTOFFSET = fontSize.height;
        if (getProgresstype() == 1) {
//            GlaNative.getInstance().calculateLayout(context, fontSize, new Rect(0, getReinitHeadshinemargin(), 0, 0));
            this.headshinemargin = fontSize.topMargin;
//            GlaNative.getInstance().calculateLayout(context, fontSize, new Rect(0, getReinitTailshinemargin(), 0, 0));
            this.tailshinemargin = fontSize.topMargin;
//            GlaNative.getInstance().calculateLayout(context, fontSize, new Rect(0, getReinitHlheadshinemargin(), 0, 0));
            this.hlheadshinemargin = fontSize.topMargin;
//            GlaNative.getInstance().calculateLayout(context, fontSize, new Rect(0, getReinitHltailshinemargin(), 0, 0));
            this.hltailshinemargin = fontSize.topMargin;
        } else {
//            GlaNative.getInstance().calculateLayout(context, fontSize, new Rect(getReinitHeadshinemargin(), 0, 0, 0));
            this.headshinemargin = fontSize.leftMargin;
//            GlaNative.getInstance().calculateLayout(context, fontSize, new Rect(getReinitTailshinemargin(), 0, 0, 0));
            this.tailshinemargin = fontSize.leftMargin;
//            GlaNative.getInstance().calculateLayout(context, fontSize, new Rect(getReinitHlheadshinemargin(), 0, 0, 0));
            this.hlheadshinemargin = fontSize.leftMargin;
//            GlaNative.getInstance().calculateLayout(context, fontSize, new Rect(getReinitHltailshinemargin(), 0, 0, 0));
            this.hltailshinemargin = fontSize.leftMargin;
        }
        if ((initmThumbwidth > 0) && (initmThumbheight > 0)) {
            Matrix matrix = new Matrix();
           /* if (initmThumbwidth != initmThumbheight)  TODO
                matrix.postScale((float) com.acloud.newinterface.GlaNative.getInstance().getFitModeDsx(), (float) com.acloud.newinterface.GlaNative.getInstance().getFitModeDsy());
            else
                matrix.postScale((float) com.acloud.newinterface.GlaNative.getInstance().getFitModeDsy(), (float) com.acloud.newinterface.GlaNative.getInstance().getFitModeDsy());
           */
            setThumb(new BitmapDrawable(context.getResources(),
                    Bitmap.createBitmap(DrawableToBitmap(initmThumb), 0, 0,
                            initmThumbwidth, initmThumbheight, matrix, true)));
        }
        if (getThumb() != null) {
            mActualWidth = this.mReinitThumbWidth = getThumb().getIntrinsicWidth();
            mActualHeight = this.mReinitThumbHeight = getThumb().getIntrinsicHeight();
        }
    }

    void onProgressRefresh(int paramInt, boolean paramBoolean, int type) {
        if (mProgressDisable == false)
            return;
        if (this.mOnSeekBarChangeListener != null) {
            if (paramInt >= getMax()) {
                paramInt = getMax();
            }
            if (paramInt <= 0) {
                paramInt = 0;
            }
            if ((getProgress() != paramInt) || (type == 1)) {
                setProgress(paramInt);
                this.mOnSeekBarChangeListener.onProgressChanged(this, getProgress(), paramBoolean);
            }
        }
    }

    protected void onSizeChanged(int paramInt1, int paramInt2, int paramInt3, int paramInt4) {
        super.onSizeChanged(paramInt1, paramInt2, paramInt3, paramInt4);
        if (getHighLightDrawable() != null) {
            if (getHighLightDrawable().getIntrinsicHeight() > 0
                    && getHighLightDrawable().getIntrinsicWidth() > 0 && paramInt1 != 0 && paramInt2 != 0) {
                this.mDrawableHight = tileify(getHighLightDrawable(),
                        getHighLightDrawable().getIntrinsicWidth(),
                        getHighLightDrawable().getIntrinsicHeight(), true);
            }

            if (mDrawableHight != null) {
                if (getProgresstype() == 1) {
                    this.mDrawableHight.setBounds(0, MARGINLEFTOFFSET, paramInt1, mViewHeight - MARGINLEFTOFFSET);
                } else {
                    this.mDrawableHight.setBounds(MARGINLEFTOFFSET, 0, mViewWith - MARGINLEFTOFFSET, paramInt2);
                }

            }
        }

        Log.i(TAG, "onSizeChanged: MARGINLEFTOFFSET:"+MARGINLEFTOFFSET+", paramInt1:"+paramInt1+",paramInt2:"+paramInt2);
        Log.i(TAG, "onSizeChanged: mViewWith:"+mViewWith+",mViewHeight:"+mViewHeight);

        if (getBackground() != null) {
            if (getProgresstype() == 1) {
                updateProgressBackgroud((int) toPX(getProgress()), paramInt1, paramInt3, paramInt4);
                getBackground().setBounds(0, 0 + MARGINLEFTOFFSET, paramInt1, mViewHeight - MARGINLEFTOFFSET);
            } else {
                updateProgressBackgroud((int) toPX(getProgress()), paramInt2, paramInt3, paramInt4);
                getBackground().setBounds(0 + MARGINLEFTOFFSET, 0, mViewWith - MARGINLEFTOFFSET, paramInt2);
            }
        }

    }

    public void drawProgress(float paramInt) {
        float xOffset = paramInt;


        int length;
        if (getProgresstype() == 1) {
            length = mViewHeight;
            updateProgressBackgroud((int) xOffset, getWidth(), 0, 0);

            if (xOffset <= MARGINLEFTOFFSET) {
                if (getDirection() == 0) {
                    onProgressRefresh(getMax(), true, 0);
                } else {
                    onProgressRefresh(0, true, 0);
                }
            } else if (xOffset >= (length - MARGINLEFTOFFSET)) {
                if (getDirection() == 0) {
                    onProgressRefresh(0, true, 0);
                } else {
                    onProgressRefresh(getMax(), true, 0);
                }
            } else {
                onProgressRefresh(Math.round(toProgress(paramInt)), true, 0);
            }
        } else {
            length = mViewWith;
            updateProgressBackgroud((int) xOffset, getHeight(), 0, 0);

            if (xOffset <= MARGINLEFTOFFSET) {
                if (getDirection() == 1) {
                    onProgressRefresh(getMax(), true, 0);
                } else {
                    onProgressRefresh(0, true, 0);
                }
            } else if (xOffset >= (length - MARGINLEFTOFFSET)) {
                if (getDirection() == 1) {
                    onProgressRefresh(0, true, 0);
                } else {
                    onProgressRefresh(getMax(), true, 0);
                }
            } else {
                onProgressRefresh(Math.round(toProgress(paramInt)), true, 0);
            }
        }
    }

    private Bitmap zoomBitmap(Bitmap bitmap, float sx, float sy, int Width, int height) {
        Matrix matrix = new Matrix();
        matrix.postScale(sx, sy);
        Bitmap resizeBmp = Bitmap.createBitmap(bitmap, 0, 0, Width, height, matrix, true);
        return resizeBmp;
    }

    void onStopTrackingTouch() {
        if (mProgressDisable == false)
            return;
        onProgressRefresh(getProgress(), true, 1);
        if (this.mOnSeekBarChangeListener != null) {
            this.mOnSeekBarChangeListener.onStopTrackingTouch(this);
        }
    }

    public boolean onTouchEvent(MotionEvent paramMotionEvent) {
        if (mProgressDisable == false)
            return true;
        switch (paramMotionEvent.getAction()) {
            case MotionEvent.ACTION_DOWN:
                onStartTrackingTouch(paramMotionEvent);
                break;
            case MotionEvent.ACTION_MOVE:
                trackTouchEvent(paramMotionEvent);
                break;
            case MotionEvent.ACTION_UP:
                trackTouchEvent_up(paramMotionEvent);
                break;
        }
        return true;
    }

    @Override
    public void setMax(int paramInt) {
        super.setMax(paramInt);
        if (getProgresstype() == 1) {
            updateProgressBackgroud((int) toPX(getProgress()), getWidth(), 0, 0);
        } else {
            updateProgressBackgroud((int) toPX(getProgress()), getHeight(), 0, 0);
        }
    }

    public void setOnSeekBarChangeListener(OnSeekBarChangeListener paramOnSeekBarChangeListener) {
        this.mOnSeekBarChangeListener = paramOnSeekBarChangeListener;
    }

    @Override
    public void setProgress(int paramInt) {
        mTouchStatus = 0;
        if (mAnimationRun == 1) {
            return;
        }
        if (paramInt == getProgress())
            return;
        super.setProgress(paramInt);
        if (getProgresstype() == 1) {
            updateProgressBackgroud((int) toPX(paramInt), getWidth(), 0, 0);
        } else {
            updateProgressBackgroud((int) toPX(paramInt), getHeight(), 0, 0);
        }
    }

    public void setVisibility(int paramInt) {
        super.setVisibility(paramInt);
    }

    @Override
    public void setSeekBarMax(int totalTime) {
        setMax(totalTime);
    }

    @Override
    public void setSeekBarProgress(int totalTime) {
        setProgress(totalTime);
    }

    public interface OnSeekBarChangeListener {
        void onProgressChanged(SeekBarEx paramSeekBar, int paramInt, boolean paramBoolean);

        void onStartTrackingTouch(SeekBarEx paramSeekBar);

        void onStopTrackingTouch(SeekBarEx paramSeekBar);
    }

    private void initAnimation() {
        if (getEffect() == 2) {
            this.mInterpolator = new LinearInterpolator();
        } else if (getEffect() == 3) {
            this.mInterpolator = new AnticipateOvershootInterpolator();
        }
    }

    private void stopProgressAnimation() {
        if (this.valueAnimator != null && this.valueAnimator.isRunning())
            this.valueAnimator.cancel();
    }

    private void startProgressAnimation(final float paramInt, final int type) {
        stopProgressAnimation();
        float startX = toPX(getProgress());
        int moveDirection = 1;
        if ((startX - paramInt) > 0)
            moveDirection = 0;
        int time;
        time = 1;//Math.abs((int)(startX-paramInt));
        if (time > 700)
            time = (int) (time * 0.35);
        else if (time > 500)
            time = (int) (time * 0.55);
        else
            time = (int) (time * 0.75);
        handler.removeMessages(SeekBarEx.VALUEX);
        if (time <= 25) {
            mAnimationRun = 1;
            setProgress(Math.round(toProgress(paramInt)));
            if (type == 1) {
                updateProgressBackgroud((int) toPX(getProgress()), getWidth(), 0, 0);
            } else {
                updateProgressBackgroud((int) toPX(getProgress()), getHeight(), 0, 0);
            }
            onStopTrackingTouch();
            handler.sendEmptyMessageDelayed(SeekBarEx.VALUEX, 350);
            return;
        }
        mAnimationRun = 1;
        int startoffset;
        int endoffset;
        int thumbLength;
        int viewLength;
        if (type == 1) {
            thumbLength = mActualHeight;
            viewLength = this.mViewHeight;
            endoffset = (int) paramInt;
        } else {
            thumbLength = mActualWidth;
            viewLength = this.mViewWith;
            endoffset = (int) paramInt;
        }
        int targetoffset = (int) toPX(Math.round(toProgress(paramInt)));
        if (moveDirection == 0) {
            startoffset = (int) (startX - thumbLength);
            if (startoffset <= (MARGINLEFTOFFSET - thumbLength / 2)) {
                startoffset = MARGINLEFTOFFSET - thumbLength / 2;
            }
            if (endoffset <= targetoffset) {
                endoffset = targetoffset;
            }
        } else {
            startoffset = (int) (startX + thumbLength);
            if (startoffset >= (MARGINLEFTOFFSET + viewLength + thumbLength / 2)) {
                startoffset = MARGINLEFTOFFSET + viewLength + thumbLength / 2;
            }
            if (endoffset >= targetoffset) {
                endoffset = targetoffset;
            }
        }
        this.valueAnimator = ValueAnimator.ofInt(new int[]{startoffset, endoffset});
        this.valueAnimator.setDuration(time);
        if (this.mInterpolator != null) {
            this.valueAnimator.setInterpolator(this.mInterpolator);
        }
        setProgress(Math.round(toProgress(paramInt)));
        this.valueAnimator.addListener(new Animator.AnimatorListener() {
            public void onAnimationCancel(Animator paramAnonymousAnimator) {
                if (type == 1) {
                    updateProgressBackgroud((int) toPX(Math.round(toProgress(paramInt))), getWidth(), 0, 0);
                } else {
                    updateProgressBackgroud((int) toPX(Math.round(toProgress(paramInt))), getHeight(), 0, 0);
                }
                mAnimationRun = 0;
            }

            public void onAnimationEnd(Animator paramAnonymousAnimator) {
                SeekBarEx.this.valueAnimator = null;
                clearAnimation();
                if (type == 1) {
                    updateProgressBackgroud((int) toPX(Math.round(toProgress(paramInt))), getWidth(), 0, 0);
                } else {
                    updateProgressBackgroud((int) toPX(Math.round(toProgress(paramInt))), getHeight(), 0, 0);
                }
                onStopTrackingTouch();
                handler.sendEmptyMessageDelayed(SeekBarEx.VALUEX, 350);
            }

            public void onAnimationRepeat(Animator paramAnonymousAnimator) {
            }

            public void onAnimationStart(Animator paramAnonymousAnimator) {
            }
        });
        this.valueAnimator.start();
        this.valueAnimator.addUpdateListener(paramAnonymousValueAnimator -> {
            if (type == 1) {
                updateProgressBackgroud((Integer) paramAnonymousValueAnimator.getAnimatedValue(), getWidth(), 0, 0);
            } else {
                updateProgressBackgroud((Integer) paramAnonymousValueAnimator.getAnimatedValue(), getHeight(), 0, 0);
            }
        });
    }
}